﻿/*
 Copyright (c) 2006 Eric J. Feminella  <eric@ericfeminella.com>
 All rights reserved.

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is furnished
 to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in all
 copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

 @internal
 */

package com.ericfeminella.system{
	import flash.system.Capabilities;
	import flash.utils.describeType;
	import flash.utils.Dictionary;

	/**
	     *
	     * All static class which provides an API for retrieving a users
	     * system capabilities
	     *
	     * @see flash.system.Capabilities
	     * @see flash.utils.describeType;
	     *
	     */
	public final class ClientSystemCapabilities {
		/**
		         *
		         * Defines a <code>Dictionary</code> which is utilized as a
		         * caching mechanism by <code>getCapabilities</code> for all
		         * subsequent calls to <code>getCapabilities</code>
		         *
		         * @see #getCapabilities
		         *
		         */
		private static var capabilitiesCache:Dictionary;

		/**
		         *
		         * Defines a <code>String</code> which is utilized as a caching
		         * mechanism by the <code>toString</code> method after the first
		         * invocation by all calls
		         *
		         * @see #toString
		         *
		         */
		private static var capabilitiesString:String;

		/**
		         *
		         * Retrieves all client system capabilities accessible from
		         * Flash Player and returns a <code>Dictionary</code> which
		         * contains the clients system settings and capabilities
		         *
		         * @return An object containing all client-system capabilities
		         * @see    import flash.system.Capabilities;
		         * @see    import flash.utils.Dictionary;
		         *
		         */
		public static function get capabilities():Dictionary {
			if (null==capabilitiesCache) {
				var systemDefinition:XML=describeType(Capabilities);
				capabilitiesCache=new Dictionary(true);

				for each (var accessor:XML in systemDefinition..accessor) {
					var prop:String=accessor.@name;
					capabilitiesCache[prop]=Capabilities[prop];
				}
			}
			return capabilitiesCache;
		}

		/**
		         *
		         * Determines each system capability accessible from Flash Player
		         * and returns a formatted <code>String</code> containing all user
		         * capabilities
		         *
		         * @return a formatted String containing all user-system capabilities
		         *
		         */
		public static function toString():String {
			if (capabilitiesString==null) {
				capabilitiesString="";

				for (var prop:String in capabilities) {
					capabilitiesString+=prop+": "+capabilities[prop]+"\n";
				}
			}
			return capabilitiesString;
		}

		/**
		         *
		         * Determines each system capability accessible from Flash Player
		         * and returns an introspecting XML object containing all user
		         * capabilities
		         *
		         * @private The return value of this method is not cached as calls
		         * to <code>toXML</code> are infrequently made
		         *
		         * @return an XML object containing all user-system capabilities
		         *
		         */
		public static function toXML():XML {
			return describeType(Capabilities);
		}
	}
}